package com.zkc.blog.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkc.blog.pojo.Blog;
import com.zkc.blog.service.BlogService;
import com.zkc.blog.uilt.ResultEntity;

@ResponseBody
@Controller
public class BlogController {
	@Autowired
	private BlogService blogServer;
	
	//浏览列表
	@GetMapping("/blog")
	public ResultEntity<PageInfo<Blog>> getBlogList(
			@RequestParam(value = "cateid", defaultValue = "0") int cateid,
			@RequestParam(value = "searchkey", defaultValue = "") String  searchkey,
			@RequestParam(value = "start", defaultValue = "0") int start,
			@RequestParam(value = "size", defaultValue = "5") int size){
		PageHelper.startPage(start,size,"blogid desc");//这句要先启动
		List<Blog> list=blogServer.getBlogList(searchkey,cateid);
		PageInfo<Blog> page = new PageInfo<>(list);//分页处理
		
		return ResultEntity.successWithoutData(page);
	} 
	
	//查看详情th:href="@{/blogs/{id}(id=${blog.id})}"
	@GetMapping("/blog/{id}")
	public ResultEntity<Blog> getBlogList(
			@PathVariable("id") Integer  id
			){
		Blog blog=blogServer.getBlog(id);
		return ResultEntity.successWithoutData(blog);
	} 
	//前端浏览列表
	@GetMapping("/blogMarkdown")
	public ResultEntity<PageInfo<Blog>> getBlogListMarkdown(
			@RequestParam(value = "searchkey", defaultValue = "") String  searchkey,
			@RequestParam(value = "start", defaultValue = "0") int start,
			@RequestParam(value = "size", defaultValue = "5") int size){
		PageHelper.startPage(start,size,"blogid desc");//这句要先启动
		List<Blog> list=blogServer.getBlogList(searchkey,0);
		PageInfo<Blog> page = new PageInfo<>(list);//分页处理
		
		return ResultEntity.successWithoutData(page);
	} 
	//前端查看详情th:href="@{/blogs/{id}(id=${blog.id})}"
	@GetMapping("/blogMarkdown/{id}")
	public ResultEntity<Blog> getBlogMarkdown(
			@PathVariable("id") Integer  id
			){
		Blog blog=blogServer.getMarkdownBlog(id);
		return ResultEntity.successWithoutData(blog);
	}
	//查看归档列表
	@GetMapping("/archive")
	public ResultEntity<PageInfo<Blog>> getBlogListByYear(
			@RequestParam(value = "year", defaultValue = "") String  year,
			@RequestParam(value = "start", defaultValue = "0") int start,
			@RequestParam(value = "size", defaultValue = "5") int size){
		PageHelper.startPage(start,size,"blog.createtime desc");//这句要先启动
		List<Blog> list=blogServer.getBlogListByYear(year);
		PageInfo<Blog> page = new PageInfo<>(list);//分页处理
		
		return ResultEntity.successWithoutData(page);
	} 
	//过去归档年份
	@GetMapping("/archive/year")
	public ResultEntity<List<String>> getBlogYear(){
		List<String> list=blogServer.getBlogYear();
		
		return ResultEntity.successWithoutData(list);
	}
	
	
	//删除(批量)
	@DeleteMapping("/blog")
	public ResultEntity<String> deleteBlogList(
			@RequestBody  List<Integer> blogids
			){
		try {
			blogServer.deleteBlogById(blogids);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	} 
	//增加
	@PostMapping("/blog")
	public ResultEntity<String> addBlog(
			@RequestBody(required = true) Blog blog
			){
		try {
			blogServer.insertBlog(blog);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	} 
	//更新（整个对象）
	@PutMapping("/blog")
	public ResultEntity<String> putBlog(
			@RequestBody(required = true) Blog blog
			){
		try {
			blogServer.updateBlogById(blog);
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(e.getMessage());
		}
		return ResultEntity.successWithoutData();
	}
	
}
